﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;

namespace Ccit.Utils
{
    /// <summary>
    /// XML 操作类
    /// </summary>
    public class XMLLib
    {
        private string _innerXML;

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="strXML"></param>
        public XMLLib(string strXML)
        {
            _innerXML = strXML;
        }

        #endregion

        /// <summary>
        /// 将XML转换成DataSet
        /// </summary>
        /// <returns></returns>
        public DataSet ConvertToDataSet()
        {
            DataSet dSet = new DataSet();
            if (string.IsNullOrEmpty(_innerXML)) return dSet;
            
            XmlDocument oDom = new XmlDocument();
            try
            {
                string strXML = _innerXML.Replace("\0", "");
                oDom.LoadXml(strXML);
                StringReader xmlText = new StringReader(oDom.DocumentElement.OuterXml);
                XmlTextReader xmlReader = new XmlTextReader(xmlText);
                dSet.ReadXml(xmlReader);
            }
            catch
            {
                
                throw new Exception("Convert XML TO DataSet Failture!");
            }
            finally
            {
                oDom = null;
            }

            return dSet;
        }
        
        /// <summary>
        /// 将XML转换成集合，作为控件的绑定数据源(如:DropDonwList、ListBox等)
        /// 并允许创建自定义标头
        /// </summary>
        /// <param name="textTagName">文本标签名称</param>
        /// <param name="valueTagName">值标签名称</param>
        /// <param name="bHasHeader">是否含有头项</param>
        /// <param name="lstHeader">头数据项对象</param>
        /// <returns></returns>
        public ListItemCollection ConvertToCollection(string textTagName,string valueTagName,bool bHasHeader,ListItem lstHeader)
        {
            ListItemCollection listItemCollection = new ListItemCollection();
            if (string.IsNullOrEmpty(_innerXML)) return listItemCollection;

            XmlDocument oDom = new XmlDocument();
            try
            {
                string strXML = _innerXML.Replace("\0", "");
                oDom.LoadXml(strXML);
                XmlNode oRoot = oDom.DocumentElement;
                //XmlNode oRow;
                string strText = "";
                string strValue = "";
                
                if(bHasHeader)  //如果有自定义头,则加上
                    listItemCollection.Add(lstHeader);

                foreach(XmlNode oRow in oRoot.ChildNodes)
                {
                    strText = oRow.SelectSingleNode(textTagName).InnerText;
                    strValue = oRow.SelectSingleNode(valueTagName).InnerText;
                    listItemCollection.Add(new ListItem(strText,strValue));
                }
            }
            catch
            {
                throw new Exception("find no xmlnode!");
            }
            finally
            {
                oDom = null;
            }

            return listItemCollection;
        }

        /// <summary>
        /// 将XML转换成集合，作为控件的绑定数据源(如:DropDonwList、ListBox等)
        /// </summary>
        /// <param name="textTagName"></param>
        /// <param name="valueTagName"></param>
        /// <returns></returns>
        public ListItemCollection ConvertToCollection(string textTagName,string valueTagName)
        {
            return ConvertToCollection(textTagName, valueTagName, false, null);
        }
    }
}
